package cn.com.elite;
import java.io.BufferedInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import javax.mail.internet.MimeUtility;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import ins.platform.interf.utils.InterfaceServletInvoker;
import ins.platform.message.vo.MailAttachVo;
import ins.platform.message.vo.MailVo;

import com.thoughtworks.xstream.XStream;
import com.thoughtworks.xstream.io.xml.DomDriver;

public class MailImplTest {
	private static final Log log=LogFactory.getLog(MailImplTest.class);
	public String SendMail(String subject,String url,String requestType,String user,String password,String fromadd,String toadd,String content,String usercode,String comcode,List list,String businesstype,String systemcode) throws IOException{
		log.info("url:"+url);
		log.info("requestType:"+requestType);
		log.info("password:"+password);
		log.info("fromadd:"+fromadd);
		log.info("toadd:"+toadd);
		log.info("content:"+content);
		log.info("usercode:"+usercode);
		log.info("comcode:"+comcode);
		log.info("businesstype:"+businesstype);
		log.info("systemcode:"+systemcode);
		String inXmlBody = "";
		String outXmlBody = "";
		InterfaceServletInvoker interfaceInvoker2 = new InterfaceServletInvoker(url);
		MailVo mailVo = new MailVo();
		mailVo.setUuid(UUID.randomUUID().toString());
		mailVo.setFromAddress(fromadd);
		mailVo.setToAddress(toadd);
//		mailVo.setCcAddress("");
//		mailVo.setSendTime(null);
		mailVo.setSystemCode(systemcode);
		mailVo.setBusinessType(businesstype);
		Calendar cal = Calendar.getInstance();
		java.util.Date sendTime = cal.getTime();
//		mailVo.setSendTime(sendTime); 
		mailVo.setSubject(subject);
		mailVo.setContent(content);
		mailVo.setUserCode(usercode);
		mailVo.setComCode(comcode);
		mailVo.setUsage("");

		if(list!=null&&list.size()>0){
			List<MailAttachVo> attachList = new ArrayList<MailAttachVo>();
			for(int i=0;i<list.size();i++){
				Map map=(Map)list.get(i);
				String path=(String)map.get("path");
				File attachFile = new File(path);
				BufferedInputStream in = new BufferedInputStream(new FileInputStream(attachFile));        
				ByteArrayOutputStream out = new ByteArrayOutputStream(1024);
				byte[] temp = new byte[1024];
				int size = 0;
				while ((size = in.read(temp)) != -1) {
					out.write(temp, 0, size);
				}
				MailAttachVo attach = new MailAttachVo();
				attach.setSerialNo(new Long("1"));
//				attach.setName(attachFile.getName());
				attach.setName(MimeUtility.encodeWord(attachFile.getName()));
				attach.setContent(out.toByteArray());
				attachList.add(attach);
				out.close();
				in.close();
			}
			mailVo.setMailInfoAttachs(attachList);		
		}
		
		XStream xstream = new XStream(new DomDriver());
		xstream.alias("Request", MailVo.class);
		inXmlBody = xstream.toXML(mailVo);
		log.info("==============输入XML================");
		log.info(inXmlBody);
		log.info("====================================");
		for (int count = 100; count > 0; count--) {
			outXmlBody = interfaceInvoker2.request(requestType,user,password,inXmlBody,"utf-8");
		}
		log.info("==============输出XML================");
		log.info(outXmlBody);
		log.info("====================================");
		return outXmlBody;
	}

	/**
	 * @param args
	 */
	public static void main(String[] args) throws FileNotFoundException, IOException {
		// TODO Auto-generated method stub
		String url = "http://109.101.85.5:7012/messageweb/interface";
		// String url = "http://localhost:8080/mw/interface";
		String inXmlBody = "";
		String outXmlBody = "";
		String requestType = "001";
		String user = "0000000000";
		String password = "pcpcncdi";
		// String xmlBody = "";
		// // 传送xmlBody
		// xmlBody += "<Request>";
		// xmlBody += "<username>demo</username>";
		// xmlBody += "<password>demo</password>";
		// xmlBody += "</Request>";
		// InterfaceServletInvoker interfaceInvoker1=new
		// InterfaceServletInvoker(url);
		// outXmlBody = interfaceInvoker1.request(requestType, user, password,
		// xmlBody);
		// System.out.println("===============输入xml===============");
		// System.out.println(outXmlBody);
		// System.out.println("====================================");
		// 传送DTO
		InterfaceServletInvoker interfaceInvoker2 = new InterfaceServletInvoker(url);
		MailVo mailVo = new MailVo();
		mailVo.setUuid(UUID.randomUUID().toString());
		mailVo.setFromAddress("qijiajing@sinosoft.com.cn");
		mailVo.setToAddress("aska.xt@163.com");
		mailVo.setCcAddress("");
		Calendar cal = Calendar.getInstance();
		java.util.Date sendTime = cal.getTime();
		mailVo.setSendTime(sendTime);
		mailVo.setSubject("2011-11-30");
		mailVo.setContent("this is a test mail ,send by tongziliang<br/>这是一封带有html代码片段的测试邮件，由童子良通过邮件服务器发送<span style='color:#ff0000;'>this is red.</span>");
		mailVo.setUserCode("0000000000");
		mailVo.setComCode("11000000");
		mailVo.setSystemCode("");
		mailVo.setUsage("");
		mailVo.setBusinessType("");

		List<MailAttachVo> attachList = new ArrayList<MailAttachVo>();
		File attachFile = new File("C:/我.txt");
		System.out.println(MimeUtility.encodeWord(attachFile.getName()));
		BufferedInputStream in = new BufferedInputStream(new FileInputStream(attachFile));        
		ByteArrayOutputStream out = new ByteArrayOutputStream(1024);
		byte[] temp = new byte[1024];
		int size = 0;
		while ((size=in.read(temp))!= -1) {
			out.write(temp,0,size);
		}
		MailAttachVo attach = new MailAttachVo();
		attach.setSerialNo(new Long("1"));
		attach.setName(MimeUtility.encodeWord(attachFile.getName()));
		attach.setContent(out.toByteArray());
		attachList.add(attach);
		out.close();
		in.close();
		mailVo.setMailInfoAttachs(attachList);
		XStream xstream = new XStream(new DomDriver());
		xstream.alias("Request", MailVo.class);
		inXmlBody = xstream.toXML(mailVo);
		System.out.println("==============输入XML================");
		System.out.println(inXmlBody);
		System.out.println("====================================");
		for (int count=100;count>0;count--) {
			outXmlBody=interfaceInvoker2.request(requestType,user,password,inXmlBody,"utf-8");
		}
		System.out.println("==============输出XML================");
		System.out.println(outXmlBody);
		System.out.println("====================================");
	}

}
